import { GlobalValue } from "../utils";

const roleHarvester = {
    /** @param {Creep} creep **/
    run: function (creep: Creep) {
        // 空了就去挖矿
        if (creep.memory.harvesting && creep.store.getFreeCapacity() === 0) {
            // 正处于挖矿状态而又背包满了，那就停止挖矿，改去工作
            creep.memory.harvesting = false;
            creep.say("🚧 work");
        } else if (
            !creep.memory.harvesting &&
            creep.store[RESOURCE_ENERGY] === 0
        ) {
            // 正在干活但是存货没了，那就去挖矿
            creep.memory.harvesting = true;
            creep.say("🔄 harvest");
        }

        if (creep.memory.harvesting) {
            // const sources = creep.room.find(FIND_SOURCES);
            const sources = GlobalValue.sources;
            if (creep.harvest(sources[0]) === ERR_NOT_IN_RANGE) {
                creep.moveTo(sources[0], {
                    visualizePathStyle: { stroke: "#ffaa00" }
                });
            }
        } else {
            // 要么去干别的
            // 找我们感兴趣的建筑

            // const targets = creep.room.find(FIND_STRUCTURES, {
            //     filter: (structure) => {
            //         return (
            //             structure.structureType === STRUCTURE_EXTENSION ||
            //             structure.structureType === STRUCTURE_SPAWN ||
            //             structure.structureType === STRUCTURE_CONTAINER ||
            //             structure.structureType === STRUCTURE_TOWER ||
            //             structure.structureType === STRUCTURE_ROAD ||
            //             structure.structureType === STRUCTURE_WALL ||
            //             structure.structureType === STRUCTURE_RAMPART
            //         );
            //     }
            // });
            const targets = GlobalValue.structures;

            const hasFreeCapacityTargets = targets.filter((structure) => {
                // 有的建筑没有store属性，比如 STRUCTURE_ROAD
                if ("store" in structure) {
                    return structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
                } else {
                    return false;
                }
            });

            if (hasFreeCapacityTargets.length > 0) {
                // 去搬运能量
                if (
                    creep.transfer(
                        hasFreeCapacityTargets[0],
                        RESOURCE_ENERGY
                    ) === ERR_NOT_IN_RANGE
                ) {
                    creep.moveTo(hasFreeCapacityTargets[0], {
                        visualizePathStyle: { stroke: "#ffffff" }
                    });
                }
            } else {
                // 去修理，我们就叫耐久值吧
                // 目前只发现container会有耐久值，但目前还是遍历上面指定类型的所有建筑
                // 初筛，指定血量阈值
                let needRepairtargets = targets.filter((structure) => {
                    // 低于50%再修，不然被堵在某一个建筑不走了
                    return structure.hits < structure.hitsMax * 0.5;
                });
                // 再筛
                needRepairtargets = _.filter(needRepairtargets, (structure) => {
                    if (
                        (structure.structureType === STRUCTURE_WALL ||
                            structure.structureType === STRUCTURE_RAMPART) &&
                        structure.hits > 10000
                    ) {
                        // 如果是WALL，修到大于10000就不修了
                        return false;
                    } else if (structure.hits > structure.hitsMax * 0.75) {
                        // 修到75%就不用修了
                        return false;
                    } else {
                        // 可以修
                        return true;
                    }
                });
                if (needRepairtargets.length > 0) {
                    const repairTarget = needRepairtargets[0];
                    // 准备修
                    if (creep.repair(repairTarget) === ERR_NOT_IN_RANGE) {
                        creep.moveTo(repairTarget, {
                            visualizePathStyle: { stroke: "#ff0000" }
                        });
                    }
                }
            }
        }
    }
};

export { roleHarvester };
